<?php
date_default_timezone_set('Asia/Shanghai');

$script_php_dir = "/soooner/cam_upload_script/php/";

$ListenPath = $script_php_dir."flvpath/";

include_once($script_php_dir."include/mcrypt.php");

eachRoot($ListenPath);
$current = "";
$flv=array();
$idx=array();
$fl="";
$id="";
function eachRoot($ListenPath=""){
global $current,$flv,$idx,$fl,$id,$script_php_dir;
$script2=array();

     if(is_dir($ListenPath)){
     $roothandle = opendir($ListenPath);
     if($roothandle!==false){
             while(false!==($file = readdir($roothandle))){
                  if($file!="."&&$file!=".."){
                        $filepath = $ListenPath.$file;
                              if(is_dir($filepath)){
             
                        $camhandle = @opendir($filepath);
					if($camhandle!==false){			
					while(false!==($file2 = readdir($camhandle))){
						if($file2!="."&&$file2!=".."){
							$filepath2 = $filepath.'/'.$file2;
								if(is_dir($filepath2)){
									$evthandle = @opendir($filepath2);
										if($evthandle!==false){
										//	$file3 = readdir($evthandle);

											while(false!==($file3 = readdir($evthandle))){
												if(strpos($file3,"flv") !== false ){
										 //	echo "$file2"."\n";
										//	if(strpos($file3,"idx" !== false)){    		
									 				$fl=$filepath2;
													$flv[$fl]=array($script_php_dir."UploadTimeAsc.php",$fl,"$file","$file2");
												}else if(strpos($file3,"idx") !== false){
													$id=$filepath2;	
													$idx[$id]=array($script_php_dir."UploadTimeAsc.php",$id,"$file","$file2");								
												}
									
											}  

										}
								}											
						}
					}
					}
}}}}}
			closedir($roothandle);
	//			print_r($flv);
	//			print_r($idx);
				$script=(array_intersect_key($flv,$idx));
				foreach($script as $b){
					$res = exec("ls $b[1] -tr | grep end.evt");
                    $startres = exec("ls $b[1] -t | grep start.evt");
                                        
                    $mp4res = exec("ls $b[1] -tr | grep .mp4.old",$rr1,$rr2);
                                        
                    echo 's_file:'.$b[1].'/'.$startres."\n";
                    echo 'e_file:'.$b[1].'/'.$res."\n";
                    if($res){
                    if(!dir_islock($b[1])){
                        dir_lock($b[1]);
                    }else{
                        remove_lock_oldtime($b[1],120);
                        continue;
                         }
                                            
                    $evt_file = $res;
                    $temp_sub = explode("-",$evt_file);
                                            
                    $start_time = date("YmdHi",strtotime(str_replace("start.evt","",$startres)))."00";
                    $end_time = date("YmdHis",(strtotime($temp_sub[0])+1));
                    echo '+'.$start_time.'-',$end_time.'-',$evt_file."\n";
                    if($mp4res){
                    $mp4evt_file = $mp4res;
                    $mp4temp_sub = explode("-",$mp4evt_file);
                    $mp4start_time = $mp4temp_sub[0];
                    $mp4end_time = str_replace(".mp4.old","",$mp4temp_sub[1]);
                    echo "-".$mp4start_time.'-',$mp4end_time."\n";

                    if((time()-strtotime($mp4end_time))>600){
                        $rres = exec("rm -rf $filepath",$rr1,$rr2);
                    } if($start_time==$mp4start_time&&$end_time==$mp4end_time){
                        dir_unlock($b[1]);
                        continue;
                    }
					}  
					}
					global $privateKey,$iv,$script_php_dir;
					$camid = $b[2];
					$shareid = $b[3];
					$input = $camid.'+'.time();
					$token = base64_en(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $input, MCRYPT_MODE_CBC, $iv));

					$t_url = "http://t.auto.soooner.com/getserver";
				            $t_data['obdid'] = 1;
                             $t_data['stype'] = 11;
                             //特殊处理  可视路况的视频
                            if(strstr($shareid,'_')){
								$t_data['stype'] = 10;
							}		
					 $t_data['token'] = $token;





					
					$script2[]=array($b[0],"$b[1]","$b[2]","$b[3]","$start_time","$end_time","$t_data");
				}	
					if($script2){
						foreach($script2 as $cmd){
						$pid=pcntl_fork();
						if($pid==-1){
							exit(-1);
						}else if($pid){
							pcntl_wait($status,WNOHANG);
						}else{
							sleep(1);
							//pcntl_exec('/usr/bin/php',$cmd);
							include_once($script_php_dir."UploadTimeAsc.php");
							exit(0);
							}
						}				
					}
			
		
				
				}
function dir_lock($path){
	if(file_exists($path)){
		$path .= "/lock";
		$fp = fopen($path,"a");
        $r=fwrite($fp,date("Y-m-d H:i:s")."\n");
		fclose($fp);
	}	
}

function remove_lock_oldtime($path,$lock_max_time){
	if(file_exists($path)){
		$path .= "/lock";
		if(file_exists($path)){
			$tmp_time = strtotime(file_get_contents($path));
			if((time()-$tmp_time)>$lock_max_time){
				unlink($path);
			}
			#unlink($path);
		}		
	}
}

function dir_unlock($path){
    if(file_exists($path)){
        $path .= "/lock";
        if(file_exists($path)){
            unlink($path);
        }
    }
}


function dir_islock($path){
	if(file_exists($path)){
		$path .= "/lock";
		return file_exists($path);
	}
}
?>	
	
